package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
@Slf4j
@RestController
@RequestMapping("/smallVideos")
public class SmallVideoController {
    @Autowired
    private SmallVideosService videosService;

    /**
     * 发布视频
     * @param videoThumbnail 封面图
     * @param videoFile 视频文件
     * @return
     */
    @CacheEvict(value="videos",allEntries = true)  //清空缓存
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }
    /**
     * 小视频列表
     */
    @Cacheable(
            value="videos",
            key = "T(com.tanhua.server.interceptor.UserHolder).getUserId()+'_'+#page+'_'+#pagesize")  //userid _ page_pagesize
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1")Integer page,
                                         @RequestParam(defaultValue = "10")Integer pagesize){
        PageResult pr = videosService.queryVideoList(page,pagesize);
        return ResponseEntity.ok(pr);
    }
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") Long userId){
        log.info("userId", userId);
        videosService.videoFocus(userId);
        return ResponseEntity.ok(null);
    }
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") Long userId){
        log.info("userId", userId);
        videosService.videoDisFocus(userId);
        return ResponseEntity.ok(null);
    }
}
